package com.buyantech.smweb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;
import javacommon.util.DateTimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.ObtainIntegral;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.ObtainIntegralManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.query.CreditsExchangeQuery;
import com.buyantech.smweb.vo.query.ExchangeTypeQuery;
import com.buyantech.smweb.vo.query.ObtainIntegralQuery;
import com.buyantech.smweb.vo.query.UserQuery;
import com.google.gson.Gson;

/**
 * 获取积分
 * @version 1.0
 * @date 2012-05-10
 */

/**
 * @author Administrator
 *
 */
@Controller
public class ObtainIntegralController extends BaseSpringController {

	private ObtainIntegralManager obtainIntegralManager;
	private UserManager userManager;
	
	private final String LIST_ACTION = "redirect:/pages/User/list.do";
 
	public void setObtainIntegralManager(ObtainIntegralManager obtainIntegralManager) {
		this.obtainIntegralManager = obtainIntegralManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}



	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
 
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,ObtainIntegralQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex"))+1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile=request.getParameter("sortField");
		if(sortfile!=null&&!sortfile.equals(""))
		  query.setSortColumns(request.getParameter("sortField")+" "+request.getParameter("sortOrder"));
		else
			 query.setSortColumns(" crdate desc");
		Page page = this.obtainIntegralManager.searchPage(query);
 
		List<ObtainIntegral> list=page.getResult();
		for(ObtainIntegral obtainIntegral:list){
			String crdate=obtainIntegral.getCrdate();
			crdate=crdate.substring(2);
			obtainIntegral.setCrdate(crdate);
		}
		
		modelMap.put("data", list);
		modelMap.put("total",page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}	
	
	
	/** 
	 * 保存新增对象
	 **/
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,ObtainIntegral obtainIntegral) throws Exception {
		String json=request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String,String> map = new HashMap<String,String>();
		map = gson.fromJson(json,Map.class);
		try {
			obtainIntegral.setUsername(map.get("username"));
			obtainIntegral.setType(2);
			obtainIntegral.setRemark(map.get("remark"));
			obtainIntegral.setCrdate(DateTimeUtil.getDateTimeString());
			if(StringUtils.isEmpty(map.get("id"))){
				ajaxPack("{state:false,msg:'用户id为空'}",request,response); return null;
			}
			if(StringUtils.isEmpty(obtainIntegral.getUsername())){
				ajaxPack("{state:false,msg:'用户名称不能为空'}",request,response); return null;
			}
			if(StringUtils.isEmpty(map.get("number"))){
				ajaxPack("{state:false,msg:'积分不能为空'}",request,response); return null;
			}
			String numberstr=map.get("number");
			if(map.get("type").equals("1")){
				numberstr="-"+numberstr;
			}
			User user=userManager.getById(map.get("id"));
			obtainIntegral.setUserid(user.getUserid());
			int number=Integer.parseInt(numberstr);
			obtainIntegral.setNumber(number);
			obtainIntegral.setFnumber(number);
			
			user.setIntegral(user.getIntegral()+number);
			user.setDintegral(user.getDintegral()+number);
			obtainIntegralManager.save(obtainIntegral);
			userManager.update(user);
		}catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
			return null;
		}
		ajaxPack("{state:true}",request,response);
		return null;
	}
	
	 /** 
	 * 执行export 
	 **/
	public ModelAndView exportData(HttpServletRequest request,HttpServletResponse response,ObtainIntegralQuery query) {
		ModelAndView result = new ModelAndView(new ExcelAVieData());
		String type="";
		List<String>headers = new ArrayList<String>();
		headers.add("用户ID");
		headers.add("用户昵称");
		headers.add("平台获得积分");
		headers.add("用户积分");
		headers.add("时间"); 
		headers.add("类型"); 
		headers.add("备注");
		headers.add("IP"); 
		headers.add("版本号"); 
		query.setPageSize(10000);
		Page page = this.obtainIntegralManager.searchPage(query);
		
		result.addObject("headers", headers);
		result.addObject("type", "3");
		result.addObject("content", page.getResult());
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Desposition","attachment; filename=demo.xls");

		return result;
	}
	
}
